package com.qk.mangement.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.common.constant.ClueStatusConstans;
import com.qk.domain.clue.ClueDO;
import com.qk.dto.clue.ClueDTO;
import com.qk.dto.clue.ClueListDTO;
import com.qk.dto.clue.UpdateClueInfoDTO;
import com.qk.entity.Clue;
import com.qk.entity.ClueQueryListByPageDto;
import com.qk.mangement.aop.anno.LogAnno;
import com.qk.mangement.service.ClueService;
import com.qk.vo.clue.ClueVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Map;


/**
 * Date:2025/10/13
 * Author:zxc
 * Description
 */
@Slf4j
@RestController
@RequestMapping("/clues")
public class ClueController {


    @Autowired
    private ClueService clueService;

    @LogAnno
    @GetMapping("/pool")
    public Result queryCluePool(ClueQueryListByPageDto clueQueryListByPageDto){
        log.info("查询线索池信息条件“{}",clueQueryListByPageDto);
        PageResult<Clue> queryListByPageVo= clueService.queryCluePool(clueQueryListByPageDto);
        return Result.success(queryListByPageVo);

    }

    @LogAnno
    @PutMapping("/false/{id}")
    public Result convertToFalse(@PathVariable Integer id, @RequestBody Map<String ,Object> requesMap){
        log.info("伪线锁id:{}", id);
        Integer reason = (Integer) requesMap.get("reason");
        String remark = (String)requesMap.get("remark");
        log.info("伪线锁原因：{}", reason);
        log.info("伪线锁备注：{}", remark);
        clueService.convertToFalse(id, reason, remark);
        return  Result.success();

    }



    @PutMapping("/toBusiness/{id}")
    public Result toBusiness(@PathVariable Integer id){
        clueService.toBusiness(id);
        return Result.success();
    }


    @LogAnno
    @PutMapping
    public Result updateClueInfoById(@RequestBody UpdateClueInfoDTO dto) {
        // TODO 验证每一个请求都会开辟一条线程池中线程处理
        log.info("=========================Controller 中 方法updateClueInfoById 处理线程名称是：{}",Thread.currentThread().getName());
        clueService.updateClueInfoById(dto);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getClueInfoById(@PathVariable Integer id){
        ClueVO vo= clueService.getClueInfoById(id) ;
        return Result.success(vo);
    }
    @LogAnno
    @PutMapping("/assign/{clueId}/{userId}")
    public Result updateClueById(@PathVariable Integer clueId, @PathVariable Integer userId){
        clueService.updateById(Clue.builder()
                .id(clueId)
                .userId(userId)
                .updateTime(LocalDateTime.now())
                .status(ClueStatusConstans.WAIT_FOLLOW_UP)
                .build());

        return Result.success();
    }




    @GetMapping
    public Result list(ClueListDTO dto) {
        log.info("查询参数：{}", "zxc");
        PageResult<ClueDO> result = clueService.clueList(dto);
        return Result.success(result);

    }

    @LogAnno
    @PostMapping
    public Result addCule(@RequestBody ClueDTO dto) {
        log.info("新增参数：{}", "zxc");
        clueService.addClue(dto);


        return Result.success();

    }
}
